//
//  ZDBWithdrawalsRecordViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/17.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBWithdrawalsRecordViewController.h"
#import "ZDBWithdrawalsTableViewCell.h"
#import "Withdrawals.h"
@interface ZDBWithdrawalsRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic,strong) NoNetworkView * workView;
@end

@implementation ZDBWithdrawalsRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self.view addSubview:self.tableView];
   
    WeakSelf
    // 上拉加载
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page =1;
        [weakSelf transactionHistroyIntegral];
    }];
    [header beginRefreshing];
    self.tableView.mj_header = header;
    
    MJRefreshBackStateFooter * foot = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page ++;
        [weakSelf transactionHistroyIntegral];
    }];
    self.tableView.mj_footer = foot;
    
}
#pragma 获取提现列表
- (void)transactionHistroyIntegral
{
    WeakSelf
    NSString  * drawingInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"drawingInfo"];
    [[SmileHttpTool sharedInstance] POST:drawingInfo parameters:@{@"type":@"",@"pageNum":@(self.page),@"pageSize":@(10)} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSDictionary * dic = responseObject;
        if (!kDictIsEmpty(dic)) {
            NSInteger  pages = [dic[@"pages"] integerValue];//一共多少条数据
            NSArray * list = dic[@"list"];//
            if (weakSelf.page>pages)
            {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }else
            {
                if (weakSelf.page > 1)
                {
                    [weakSelf.dataSource addObjectsFromArray:[Withdrawals mj_objectArrayWithKeyValuesArray:list]];
                    
                }else
                {
                    self.dataSource = [Withdrawals mj_objectArrayWithKeyValuesArray:list];
                }
            }
            [self.tableView reloadData];
        }else
        {
            
            [JMNotifyView showNotify:message isSuccessful:NO];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
                                 [self placeholderViewWithFrame:self.tableView.frame NoNetwork:NO];
                             });
    } failure:^(id responseObject, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - kStatusBarAndNavigationBarHeight)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = CELLCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.0001)];
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBWithdrawalsTableViewCell" bundle:nil]  forCellReuseIdentifier:@"ZDBWithdrawalsTableViewCell"];
    
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBWithdrawalsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBWithdrawalsTableViewCell" forIndexPath:indexPath];
    cell.withdrawals = _dataSource[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)placeholderViewWithFrame:(CGRect)frame NoNetwork:(BOOL)NoNetwork
{
    if (self.dataSource.count == 0)
    {
        [_workView removeFromSuperview];
        _workView = [[NoNetworkView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) NoNetwork:NoNetwork];
        [self.tableView addSubview:_workView];
        //_workView.centerY = self.collectionView.centerY-100;
    }else
    {
        [_workView dissmiss];
    }
    
}

@end
